/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mygame.Backbone;

import java.awt.Color;
import java.util.ArrayList;

import mygame.Manager;

import com.jme3.asset.AssetManager;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Node;
import com.jme3.shadow.DirectionalLightShadowRenderer;
import com.jme3.shadow.EdgeFilteringMode;

/**
 *
 * @author Tobi
 */
public class LightManager {

    private Node LightNode;
    private ViewPort viewPort;
    private AssetManager asm;
    private ArrayList<DirectionalLight> directionalLights;
    private ArrayList<AmbientLight>  ambientLights;

    public LightManager(Node root, ViewPort viewPort, AssetManager asm) {
    	this.directionalLights = new ArrayList<>();
    	this.ambientLights = new ArrayList<>();
        this.LightNode = root;
        this.viewPort = viewPort;
        this.asm = asm;
    }

    public void init() {
        /** A white ambient light source. */
        AmbientLight ambient = new AmbientLight();
        ambient.setColor(new ColorRGBA(0.4f, 0.4f, 0.4f,1));
        LightNode.addLight(ambient);
        ambientLights.add(ambient);
        
        
//        PointLight p = new PointLight();
//        p.setRadius(10f);
//        p.setPosition(new Vector3f(0, 3, 0));
//        LightNode.addLight(p);
        DirectionalLight sun = new DirectionalLight();
        sun.setColor(ColorRGBA.White);
        sun.setDirection(new Vector3f(-.5f,-.5f,-.5f).normalizeLocal());
        LightNode.addLight(sun);
        directionalLights.add(sun);
        
        enableShadowRendering(512);
    }
    
    public DirectionalLight getDirectionalLight(int index){
    	return this.directionalLights.get(index);
    }
    
    private void enableShadowRendering(int shadowmapsize){
    	DirectionalLightShadowRenderer dlsr = new DirectionalLightShadowRenderer(asm, shadowmapsize, 3);
        dlsr.setLight(getDirectionalLight(0));
        dlsr.setLambda(0.55f);
        dlsr.setShadowIntensity(0.3f);
        dlsr.setEdgeFilteringMode(EdgeFilteringMode.Dither);
        //dlsr.displayDebug();
        viewPort.addProcessor(dlsr);
    }
}
